<template>
	<view class="container">
		<navbar :config="config" backColor="#000"></navbar>
		<view class="list">
			<radio-group>
				<view class="item " v-for="(item,index) in addressList" :key="item.id">
					<view class="userInfo" @click="backSurePay(item.id)">
						<view class="street_name">{{item.street_name}}{{item.community_name}}{{item.address}}</view>
						<view class="namePhone">
						{{item.province_name}}{{item.city_name}}{{item.area_name}}
						</view>
						<view>
							{{item.name}}{{item.phone}}
						</view>
					</view>
					<view class="operation ">
						<!-- <view class="u-flex-center" @click="isDefault(item.id)">
						<view class="kuang"> -->
						<!-- <radio color="#FF0000" style="transform:scale(0.7)" :checked="item.is_default" /> -->
						<!-- <image v-if="item.is_default" :src="imgUrl+'/checked.png'" mode="widthFix" style="width: 30rpx;height: 30rpx;margin-right: 10rpx;"></image>
						<image v-else :src="imgUrl+'/nocheck.png'" mode="widthFix" style="width: 50rpx;height: 50rpx;"></image> -->
						<!-- <checkbox color="#FF0000" style="transform:scale(0.7)" :checked="item.is_default" /> -->
						<!-- <radio v-else style="transform:scale(0.7)" :checked="false" /> -->
						<!-- </view>
					</view> -->
						<view class="whole u-flex-center" @click="isDefault(item.id)">
							<view class="top" v-if="item.is_default">
								<icon type="success" color="#FF0000" size="17"></icon>
							</view>
							<view  v-else
								></view>
							<text style="font-weight: 400 !important;font-size: 23rpx !important;color: #333333 !important;">
								{{item.is_default?'默认收货地址':'设为默认'}}
							</text>
						</view>
						<view class="u-flex-center editSelect">
							<view  @click="edit(item.id)" class="editView" style="margin-right: 38rpx;">编辑</view>
							<view @click="del(item.id)" class="editView">删除</view>
						</view>
					</view>
				</view>
			</radio-group>
		</view>
		<!-- 没有数据 -->
		<view class="nogoods u-mt30 u-flex-column-center" v-if="addressList.length==0">
			<nodata :config="{top:5,content:'您还没有添加地址~'}"></nodata>
		</view>
		<view class="add-btn" @click="goAddEditAddress">
			<view class="u-plr30">
				<button class=" u-FFF">添加新地址</button>
			</view>

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					back: true, //false是tolbar页面 是则不写
					title: '收货地址',
					color: '#000',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, "#fff"],
					statusBarFontColor: '#1A1A1A',
					backIconColor: "#000"
				},
				addressList: [],
				imgUrl: this.$mConfig.staticUrl
			}
		},
		onLoad() {


		},
		onShow() {
			this.$nextTick(() => {
				this.getAddressList()
			})

		},
		methods: {
			//获取地址列表
			async getAddressList() {
				let that = this
				this.$http.get('/useraddress/list', {}).then(async res => {
					console.log('res', res)
					if (res.code == 200) {
						that.addressList = res.list
					}
				})
			},
			listAddress(e) {
				if (e == 1) {
					this.getAddressList()
				}
			},
			//跳转确认下单页面
			backSurePay(id) {
				// uni.setStorageSync('address',item)
				let object = {
					addressid: id
				}
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];
				if (prevPage.$vm.otherFun) {
					prevPage.$vm.otherFun(object)
				}
				uni.navigateBack({
					delta: 1
				})
				// uni.navigateTo({
				// 	url:'/pages/product/surePay/surePay?addressid='+id
				// })
			},
			//跳转到添加地址
			goAddEditAddress() {
				uni.navigateTo({
					url: "addEditAddress"
				})
			},
			isDefault(id) {
				let that = this
				this.$http.put('/useraddress/set-def/' + id).then(async res => {
					if (res && res.code == 200) {
						uni.showToast({
							title: "修改成功",
							icon: "none",
							duration: 3000,
							success() {
								that.getAddressList()
								that.addressList = []
							}
						})
					}
				})
				// if(e.is_default == true){
				// 	e.is_default = false
				// 	that.update(e)
				// }else{
				// 	e.is_default =true
				// 	that.update(e)
				// }

			},
			update(param) {

			},
			//编辑
			edit(id) {
				uni.navigateTo({
					url: "addEditAddress?id=" + id
				})
			},
			//删除
			async del(id) {
				let that = this
				uni.showModal({
					title: '',
					content: "是否删除这条地址",
					confirmColor: "#FF721B",
					cancelColor: "#474747",
					success: (res) => {
						if (res.confirm) {
							this.$http.delete('/useraddress/delete/' + id, )
								.then(async res => {
									if (res.code == 200) {
										uni.showToast({
											title: "删除成功",
											icon: "none",
											duration: 3000,
											success() {
												that.getAddressList()
											}
										})
									}
								})
						}
					}
				})

			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F5F5F5;
	}

	.kuang {
		display: flex;
		align-items: center;
	}

	.list {
		margin-bottom: 160rpx;
	}

	.item {
		width: 712rpx;
		height: auto;
		border-radius: 19rpx 19rpx 19rpx 19rpx;
		
		.street_name{
			font-weight: 400;
			font-size: 27rpx;
			color: #333333;
			line-height: 27rpx;
		}
		.namePhone{
			font-weight: 600;
			font-size: 31rpx;
			color: #333333;
			line-height: 31rpx;
			margin-top: 23rpx;
			margin-bottom: 23rpx;
		}
		.operation {
			margin-top: 23rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 0 39rpx;
			width: 100%;
			box-sizing: border-box;
			margin-left:19rpx;
			.whole {
				display: flex;
				align-items: center;
			}

			/deep/ .uni-icon-success {
				color: RGB(255,33,31) !important;
			}

			.top {
				display: flex;
				align-items: center;
				margin-right: 10rpx;
			}
		}
	}

	.add-btn {
		position: fixed;
		bottom: 31rpx;
		
		width: 673rpx;
		height: 96rpx;
		background: #FF211F;
		border-radius: 19rpx 19rpx 19rpx 19rpx;
		margin-left: 38rpx;
		font-weight: 400;
		font-size: 29rpx;
		color: #FFFFFF;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		

		button {
			width: 100%;
		}
	}
	.editView{
		font-weight: 400;
		font-size: 23rpx;
		color: #333333;
	}
	.editSelect{
	}
	.userInfo{
		width: 712rpx;
		height: 192rpx;
		background: #FFFFFF;
		border-radius: 19rpx 19rpx 19rpx 19rpx;
		margin:19rpx 19rpx 0 19rpx;
		padding-top:31rpx;
		padding-left: 19rpx;
		padding-bottom: 31rpx;
		box-sizing: border-box;
	}
</style>